package leetcode.code1090;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int largestValsFromLabels(int[] values, int[] labels, int numWanted, int useLimit) {
		int len = values.length;
		Integer[] s = new Integer[len];
		for (int i = 0; i < s.length; i++) {
			s[i] = i;
		}
		Arrays.sort(s, (a, b) -> values[b] - values[a]);
		Map<Integer, Integer> cnts = new HashMap<>();
		int ans = 0, sum = 0;
		for (int i = 0; i < s.length && sum < numWanted; i++) {
			int v = values[s[i]], l = labels[s[i]];
			int show = cnts.getOrDefault(l, 0);
			if (show < useLimit) {
				ans += v;
				cnts.put(l, show + 1);
				sum++;
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(6, this.largestValsFromLabels(H.str2array3("[2,6,1,2,6]"), H.str2array3("[2,2,2,1,1]"), 1, 1));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
